package com.zros.modules.business.controller;


import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zros.common.api.vo.PageResult;
import com.zros.common.exception.JeecgBootException;
import com.zros.modules.business.dto.QueryStatisticDto;
import com.zros.modules.business.service.IStatisticService;
import com.zros.modules.business.vo.StatisticVo;
import com.zros.modules.shiro.authc.util.JwtUtil;
import com.zros.modules.system.dto.UserInfo;
import com.zros.modules.system.service.IUserService;

/**
 * <p>
 * 统计查询 前端控制器
 * </p>
 *
 * @author shimiao
 * @since 2019-04-24
 */
@RestController
@RequestMapping("/business/statistic")
public class StatisticController {
	
	@Value("${rank.queryKey.company}")
	private String queryKeyCompany;
	
	@Value("${rank.queryKey.campus}")
	private String queryKeyCampus;
	
	@Value("${rank.queryKey.self}")
	private String queryKeySelf;
	
	@Value("${authority.companyKey}")
	private String authorityCompanyKey;
	
	@Value("${authority.campusKey}")
	private String authorityCampusKey;
	
	@Value("${authority.selfKey}")
	private String authoritySelfKey;
	
	@Autowired
	private IStatisticService statisticService;
	
	@Autowired
	private IUserService userService;
	
	/**
	 *  获取当前用户
	 */
	UserInfo queryCurrentUser(HttpServletRequest request)
	{
		UserInfo currentUser = new UserInfo();
		String userAccount = JwtUtil.getUserAccountByToken(request);
		currentUser = userService.queryUserInfoByAccount(userAccount,"queryClueOrStatistic");
		
		return currentUser;
	}
	
	/**
	 * 业绩排行统计
	 */
	@ApiOperation(value = "业绩排行统计")
	@RequiresPermissions("business:statistic:performanceRanking")
	@RequestMapping(value = "/performanceRanking", method = RequestMethod.GET)
	public PageResult<StatisticVo> queryRanking(HttpServletRequest request, Page<StatisticVo> page, QueryStatisticDto queryStatisticDto) 
	{
		//业绩排行不分页
		page.setSize(999999);
		
		Page<StatisticVo> pageStatistic = new Page<StatisticVo>();
		
		//获取当前用户
		UserInfo currentUser = queryCurrentUser(request);
		//获取数据权限key
		String dataResourceKey = currentUser.getDataResourceKey();
		if(StringUtils.isEmpty(dataResourceKey))
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		delDtoValue(queryStatisticDto);
		
		//业绩排行查询key
		String queryKey = queryStatisticDto.getQueryKey();
		
		//查询key为空，默认根据数据权限查询
		if(StringUtils.isEmpty(queryKey))
		{
			//数据权限为自身，查询自身数据
			if(authoritySelfKey.equals(dataResourceKey))
			{
				queryStatisticDto.setOwnerId(currentUser.getId());
				pageStatistic = statisticService.querySelfPersonRankList(page, queryStatisticDto);
			}
			//数据权限为校区，查询自身所属校区数据
			else if(authorityCampusKey.equals(dataResourceKey))
			{
				queryStatisticDto.setUserId(currentUser.getId());
				pageStatistic = statisticService.querySelfCampusRankList(page, queryStatisticDto);
			}
			//数据权限为公司，查询公司数据
			else if(authorityCompanyKey.equals(dataResourceKey))
			{
				pageStatistic = statisticService.queryCompanyRankList(page, queryStatisticDto);
			}
			else
			{
				throw new JeecgBootException("数据权限配置不正确！请联系管理员！");
			}
		}
		//查询key为3,查询员工数据
		else if(queryKeySelf.equals(queryKey))
		{
			//数据权限为自身，查询自身数据
			if(authoritySelfKey.equals(dataResourceKey))
			{
				queryStatisticDto.setOwnerId(currentUser.getId());
				pageStatistic = statisticService.querySelfPersonRankList(page, queryStatisticDto);
			}
			//数据权限为校区，查询自身所属校区用户数据
			else if(authorityCampusKey.equals(dataResourceKey))
			{
				queryStatisticDto.setUserId(currentUser.getId());
				pageStatistic = statisticService.querySelfCampusPersonRankList(page, queryStatisticDto);
			}
			//数据权限为公司，查询所有用户数据
			else if(authorityCompanyKey.equals(dataResourceKey))
			{
				pageStatistic = statisticService.queryAllPersonRankList(page, queryStatisticDto);
			}
			else
			{
				throw new JeecgBootException("数据权限配置不正确！请联系管理员！");
			}
		}
		//查询key为2,查询校区数据
		else if(queryKeyCampus.equals(queryKey))
		{
			//数据权限为校区，查询自身所属校区数据
			if(authorityCampusKey.equals(dataResourceKey))
			{
				queryStatisticDto.setUserId(currentUser.getId());
				pageStatistic = statisticService.querySelfCampusRankList(page, queryStatisticDto);
			}
			//数据权限为公司，查询所有校区数据
			else if(authorityCompanyKey.equals(dataResourceKey))
			{
				pageStatistic = statisticService.queryAllCampusRankList(page, queryStatisticDto);
			}
			else
			{
				throw new JeecgBootException("数据权限配置不正确！请联系管理员！");
			}
		}
		//查询key为1，查询公司数据
		else
		{
			//数据权限为公司，查询所有校区数据
			if(authorityCompanyKey.equals(dataResourceKey))
			{
				pageStatistic = statisticService.queryCompanyRankList(page, queryStatisticDto);
			}
			else
			{
				throw new JeecgBootException("数据权限配置不正确！请联系管理员！");
			}
		}
		
		List<StatisticVo> StatList = pageStatistic.getRecords();
		
		Integer i = 1;
		for(StatisticVo statisticVo : StatList)
		{
			statisticVo.setRankNum(i.toString());
			i++;
		}
		
		PageResult<StatisticVo> pageResult = new PageResult<StatisticVo>(pageStatistic.getTotal(), StatList);
		return pageResult;
	}
	
	/**
	 * 校区到访率统计
	 */
	@ApiOperation(value = "校区到访率统计")
	@RequiresPermissions("business:statistic:campusVisit")
	@RequestMapping(value = "/campusVisit", method = RequestMethod.GET)
	public PageResult<StatisticVo> queryCampusVisit(HttpServletRequest request, Page<StatisticVo> page, QueryStatisticDto queryStatisticDto) 
	{
		Page<StatisticVo> pageStatistic = new Page<StatisticVo>();
		
		//获取当前用户
		UserInfo currentUser = queryCurrentUser(request);
		//获取数据权限key
		String dataResourceKey = currentUser.getDataResourceKey();
		if(StringUtils.isEmpty(dataResourceKey))
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}	
		
		delDtoValue(queryStatisticDto);
		
		//数据权限为校区，查询自身所属校区数据
		if(authorityCampusKey.equals(dataResourceKey))
		{
			queryStatisticDto.setUserId(currentUser.getId());
			pageStatistic = statisticService.queryCampusVisitConList(page, queryStatisticDto);
		}
		//数据权限为公司，查询公司数据
		else if(authorityCompanyKey.equals(dataResourceKey))
		{
			pageStatistic = statisticService.queryCampusVisitConList(page, queryStatisticDto);
		}
		else
		{
			throw new JeecgBootException("数据权限配置不正确！请联系管理员！");
		}
		
		PageResult<StatisticVo> pageResult = new PageResult<StatisticVo>(pageStatistic.getTotal(), pageStatistic.getRecords());
		return pageResult;
	}
	
	/**
	 * 校区到访转成交率统计
	 */
	@ApiOperation(value = "校区到访转成交率统计")
	@RequiresPermissions("business:statistic:campusVisitRate")
	@RequestMapping(value = "/campusVisitRate", method = RequestMethod.GET)
	public PageResult<StatisticVo> queryCampusVisitRate(HttpServletRequest request, Page<StatisticVo> page, QueryStatisticDto queryStatisticDto) 
	{
		Page<StatisticVo> pageStatistic = new Page<StatisticVo>();
		
		//获取当前用户
		UserInfo currentUser = queryCurrentUser(request);
		//获取数据权限key
		String dataResourceKey = currentUser.getDataResourceKey();
		if(StringUtils.isEmpty(dataResourceKey))
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		delDtoValue(queryStatisticDto);
		
		//数据权限为校区，查询自身所属校区数据
		if(authorityCampusKey.equals(dataResourceKey))
		{
			queryStatisticDto.setUserId(currentUser.getId());
			pageStatistic = statisticService.queryCampusVisitRateConList(page, queryStatisticDto);
		}
		//数据权限为公司，查询公司数据
		else if(authorityCompanyKey.equals(dataResourceKey))
		{
			pageStatistic = statisticService.queryCampusVisitRateConList(page, queryStatisticDto);
		}
		else
		{
			throw new JeecgBootException("数据权限配置不正确！请联系管理员！");
		}
		
		PageResult<StatisticVo> pageResult = new PageResult<StatisticVo>(pageStatistic.getTotal(), pageStatistic.getRecords());
		return pageResult;
	}
	
	/**
	 * 老师试听报名率统计
	 */
	@ApiOperation(value = "老师试听报名率统计")
	@RequiresPermissions("business:statistic:teacherEnrollRate")
	@RequestMapping(value = "/teacherEnrollRate", method = RequestMethod.GET)
	public PageResult<StatisticVo> queryTeacherEnrollRate(HttpServletRequest request, Page<StatisticVo> page, QueryStatisticDto queryStatisticDto) 
	{
		Page<StatisticVo> pageStatistic = new Page<StatisticVo>();
		
		//获取当前用户
		UserInfo currentUser = queryCurrentUser(request);
		//获取数据权限key
		String dataResourceKey = currentUser.getDataResourceKey();
		if(StringUtils.isEmpty(dataResourceKey))
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		delDtoValue(queryStatisticDto);
		
		//数据权限为校区，查询自身所属校区数据
		if(authorityCampusKey.equals(dataResourceKey))
		{
			queryStatisticDto.setUserId(currentUser.getId());
			pageStatistic = statisticService.queryTeacherEnrollRateConList(page, queryStatisticDto);
		}
		//数据权限为公司，查询公司数据
		else if(authorityCompanyKey.equals(dataResourceKey))
		{
			pageStatistic = statisticService.queryTeacherEnrollRateConList(page, queryStatisticDto);
		}
		else
		{
			throw new JeecgBootException("数据权限配置不正确！请联系管理员！");
		}
		
		PageResult<StatisticVo> pageResult = new PageResult<StatisticVo>(pageStatistic.getTotal(), pageStatistic.getRecords());
		return pageResult;
	}
	
	/**
	 * 用户到访率统计
	 */
	@ApiOperation(value = "用户到访率统计")
	@RequiresPermissions("business:statistic:userVisit")
	@RequestMapping(value = "/userVisit", method = RequestMethod.GET)
	public PageResult<StatisticVo> queryUserVisit(HttpServletRequest request, Page<StatisticVo> page, QueryStatisticDto queryStatisticDto) 
	{
		Page<StatisticVo> pageStatistic = new Page<StatisticVo>();
		
		//获取当前用户
		UserInfo currentUser = queryCurrentUser(request);
		//获取数据权限key
		String dataResourceKey = currentUser.getDataResourceKey();
		if(StringUtils.isEmpty(dataResourceKey))
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		delDtoValue(queryStatisticDto);
		
		//数据权限为自身，查询自身数据
		if(authoritySelfKey.equals(dataResourceKey))
		{
			queryStatisticDto.setOwnerId(currentUser.getId());
			pageStatistic = statisticService.queryUserVisitConList(page, queryStatisticDto);
		}
		//数据权限为校区，查询自身所属校区用户数据
		else if(authorityCampusKey.equals(dataResourceKey))
		{
			queryStatisticDto.setUserId(currentUser.getId());
			pageStatistic = statisticService.queryUserVisitConList(page, queryStatisticDto);
		}
		//数据权限为公司，查询公司所有用户数据
		else if(authorityCompanyKey.equals(dataResourceKey))
		{
			pageStatistic = statisticService.queryUserVisitConList(page, queryStatisticDto);
		}
		else
		{
			throw new JeecgBootException("数据权限配置不正确！请联系管理员！");
		}
		
		PageResult<StatisticVo> pageResult = new PageResult<StatisticVo>(pageStatistic.getTotal(), pageStatistic.getRecords());
		return pageResult;
	}
	
	/**
	 * 用户到访转成交率统计
	 */
	@ApiOperation(value = "用户到访转成交率统计")
	@RequiresPermissions("business:statistic:userVisitRate")
	@RequestMapping(value = "/userVisitRate", method = RequestMethod.GET)
	public PageResult<StatisticVo> queryUserVisitRate(HttpServletRequest request, Page<StatisticVo> page, QueryStatisticDto queryStatisticDto) 
	{
		Page<StatisticVo> pageStatistic = new Page<StatisticVo>();
		
		//获取当前用户
		UserInfo currentUser = queryCurrentUser(request);
		//获取数据权限key
		String dataResourceKey = currentUser.getDataResourceKey();
		if(StringUtils.isEmpty(dataResourceKey))
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		delDtoValue(queryStatisticDto);
		
		//数据权限为自身，查询自身数据
		if(authoritySelfKey.equals(dataResourceKey))
		{
			queryStatisticDto.setOwnerId(currentUser.getId());
			pageStatistic = statisticService.queryUserVisitRateConList(page, queryStatisticDto);
		}
		//数据权限为校区，查询自身所属校区用户数据
		else if(authorityCampusKey.equals(dataResourceKey))
		{
			queryStatisticDto.setUserId(currentUser.getId());
			pageStatistic = statisticService.queryUserVisitRateConList(page, queryStatisticDto);
		}
		//数据权限为公司，查询公司所有用户数据
		else if(authorityCompanyKey.equals(dataResourceKey))
		{
			pageStatistic = statisticService.queryUserVisitRateConList(page, queryStatisticDto);
		}
		else
		{
			throw new JeecgBootException("数据权限配置不正确！请联系管理员！");
		}
		
		PageResult<StatisticVo> pageResult = new PageResult<StatisticVo>(pageStatistic.getTotal(), pageStatistic.getRecords());
		return pageResult;
	}
	
	/**
	 *  清空dto中多余属性方法
	 */
	void delDtoValue(QueryStatisticDto queryStatisticDto)
	{
		if(!StringUtils.isEmpty(queryStatisticDto.getChannelTypeId()) && !StringUtils.isEmpty(queryStatisticDto.getChannelId()))
		{
			queryStatisticDto.setChannelTypeId("");
		}
	}
	
}
